package com.atguigu.tingshu.common.login;

import com.alibaba.nacos.common.utils.StringUtils;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.netty.util.internal.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.awt.*;

/**
 * @author Wu Xingbin
 * @date 2023/11/17/16:51
 */
@Aspect
@Component
public class GuiGuLoginAspect {
    @Autowired
    private RedisTemplate redisTemplate;

    @SneakyThrows
    @Around("execution(* com.atguigu.tingshu.*.api.*.*(..)) && @annotation(GuiGuLogin)")
    public Object loginAspect(ProceedingJoinPoint proceedingJoinPoint, GuiGuLogin GuiGuLogin) {
        //获取请求对象
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //转化ServletRequestAttributes
        ServletRequestAttributes sra = (ServletRequestAttributes) requestAttributes;
        //获取到HttpServletRequest
        HttpServletRequest request = sra.getRequest();
        //获取token
        String token = request.getHeader("token");
        //判断是否需要登录
        if (GuiGuLogin.required()) {
            //必须是要登录的，如果token是空的话就抛出异常
            if (StringUtils.isEmpty(token)) {
                //空的，抛出异常
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
            //如果token不是空的，得从缓存中获取信息
            UserInfoVo userInfoVo =
                    (UserInfoVo) this.redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
            //判断对象是否为空
            if (userInfoVo == null) {
                throw new GuiguException(ResultCodeEnum.LOGIN_AUTH);
            }
        }

        //不需要强制登录，但是可能需要信息
        if (!StringUtils.isEmpty(token)) {
            //如果token不为空的话，从缓存中获取信息
            UserInfoVo userInfoVo =
                    (UserInfoVo) this.redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
            //如果token是空的话，那就将信息存到请求头中
            if (userInfoVo != null) {
                AuthContextHolder.setUserId(userInfoVo.getId());
                AuthContextHolder.setUsername(userInfoVo.getNickname());
            }
        }

        return proceedingJoinPoint.proceed();
    }
}
